package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;

public class CustomJButton extends JButton {
	
	/**
	 * Class: CustomJButton
	 * Description: customed JButon
	 * @category GUI
	 * @author Mr3chi
	 * @since 04/04/2013
	 *
	 */
	private static final long serialVersionUID = -7950620115713394763L;
	private String icon;
	private String hoverIcon;
	private String clickedIcon;
	
	public CustomJButton(String icon, String hoverIcon, String clickedIcon, int width, int height, String tooltip) throws IOException {
		this.icon = icon;
		this.hoverIcon = hoverIcon;
		this.clickedIcon = clickedIcon;
		
		try {
			Image temp = loadImage(this.icon).getScaledInstance(20, 20, 5);
			setIcon(new ImageIcon(temp));
			temp = loadImage(this.hoverIcon).getScaledInstance(20, 20, 5);
			setRolloverIcon(new ImageIcon(temp));
			temp = loadImage(this.clickedIcon).getScaledInstance(20, 20, 5);
			setPressedIcon(new ImageIcon(temp));
		} catch (Exception e) {
			throw new IOException("Lỗi không nạp được ảnh của nút " + this.getName());
		}
		
		setPreferredSize(new Dimension(width, height));
		setOpaque(false);
		setFocusPainted(false);
		setContentAreaFilled(false);
		setBorderPainted(false);
		setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
		if (!tooltip.equalsIgnoreCase("")) {
			setToolTipText(tooltip);
		}
		
		setForeground(Color.WHITE);
	}
	
	private BufferedImage loadImage(String dir) throws IOException{
        return ImageIO.read(new File(dir));
    }
}
